$(function () {
    $('#client_plan_datagrid').datagrid({
        fit:true,
        url:'/clientPlan/selectAllPlan.do',
        pagination:true,
        fitColumns:true,
        toolbar:"#client_plan_toolbar",
        singleSelect:true,
        rownumbers:true,
        columns:[[
            {field:'id',title:'编号',width:100,hidden:true},
            {field:'result_id',title:'标记',width:100,formatter:function (value, row, index) {
                if(row.result_id != null){
                    if(row.result_id == 1){
                        return "<font style='color: darkred'>差</font>";
                    }else if(row.result_id == 2){
                        return "<font style='color: sandybrown'>良</font>";
                    }else if(row.result_id == 3){
                        return "<font style='color: green'>优</font>";
                    }
                }else {
                    return "<font style='color: red'>暂未标记</font>";
                }
            }},
            {field:'planTime',title:'计划时间',width:100},
            {field:'inputUser',title:'录入人',width:100,formatter:function (value, row, index) {
                    return row.inputUser.username;
            }},
            {field:'name',title:'客户姓名',width:100},
            {field:'theme',title:'主题',width:100},
            {field:'way',title:'方式',width:100},
            {field:'detail',title:'详情',width:100},
            {field:'remark',title:'备注',width:100}
        ]]
    });

    $("#client_sign_dialog").dialog({
        height:200,
        width:350,
        buttons:"#client_sign_button",
        closed:true
    });

    $("#client_save_dialog").dialog({
        height:430,
        width:550,
        buttons:"#client_save_button",
        closed:true
    });

    $("#client_list_dialog").dialog({
        width:1800,
        height:400,
        buttons:"#client_list_button",
        closed:true
    });

    $('#client_list_datagrid').datagrid({
        fit:true,
        //width:500,
        //height:400,
        url:'/client/selectByStatus.do?status=0',
        pagination:true,
        fitColumns:true,
        singleSelect:true,
        columns:[[
            {field:'id',title:'编号',width:100},
            {field:'inputTime',title:'录入时间',width:100},
            {field:'inputUser',title:'录入人',width:100,formatter:function (value, row, index) {
                return row.inputUser.username;
            }},
            {field:'takeUser',title:'负责人',width:100,formatter:function (value, row, index) {
                if(row.takeUser != null){
                    return row.takeUser.username;
                }else{
                    return "<font style='color: red'>暂无负责人</font>";
                }
            }},
            {field:'name',title:'客户名称',width:100},
            {field:'age',title:'年龄',width:100},
            {field:'sex',title:'性别',width:100,formatter:function (value, row, index) {
                if(row.sex){
                    return "男";
                }else{
                    return "女";
                }
            }},
            {field:'job',title:'职位',width:100},
            {field:'salary',title:'收入',width:100},
            {field:'tel',title:'电话',width:100},
            {field:'email',title:'邮箱',width:100},
            {field:'address',title:'地址',width:100},
            {field:'source',title:'客源',width:100},
            {field:'qq',title:'QQ',width:100},
            {field:'id_card',title:'身份证',width:100},
            {field:'status',title:'状态',width:100,formatter:function (value, row, index) {
                if(row.status == 0){
                    return "潜在客户";
                }else if(row.status == 1){
                    return "正式客户";
                }else if(row.status == 3){
                    return "资源池客户";
                }
            }}
        ]]
    });
});

function choose() {
    var row = $('#client_list_datagrid').datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    var name = row.name;

    var rows = $('#client_plan_datagrid').datagrid('getRows');
    for(var i=0;i<=rows.length-1;i++){
        if(rows[i].name==name){
            $.messager.alert('温馨提示','该潜在用户已有开发计划，请在信息表中查看哦~','info');
            return;
        }
    }

    $("#client_save_form").form('load',{name:name});
    $("#client_list_dialog").dialog('close');
}

function list() {
    $("#client_list_dialog").dialog('setTitle',"潜在客户列表");
    $("#client_list_dialog").dialog('open');
}

function open_save() {
    $("#client_save_dialog").dialog('setTitle','新增计划');
    $("#client_save_form").form('clear');
    $("#client_save_dialog").dialog('open');
}

function open_edit() {
    var row = $("#client_plan_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    $("#client_save_form").form('load',row);
    $("#client_save_dialog").dialog('setTitle','编辑计划');
    $("#client_save_dialog").dialog('open');
}


function open_sign() {
    var row = $("#client_plan_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    $("#client_sign_dialog").dialog('setTitle','标记');
    $("#client_sign_form").form('load',row);
    $("#client_sign_dialog").dialog('open');
}

function sign() {
    $("#client_sign_form").form('submit',{
        url: '/clientPlan/sign.do',
        success:function (data) {
            var data = $.parseJSON(data);
            if(data.success){
                $("#client_sign_dialog").dialog('close');
                $.messager.alert("温馨提示",data.msg,'info',function () {
                    $("#client_plan_datagrid").datagrid('reload');
                });
            }else{
                $.messager.alert("温馨提示",data.msg,'error');
            }
        }
    })
}

function save() {
    $("#client_save_form").form('submit',{
        url:'/clientPlan/saveOrUpdate.do',
        success:function (data) {
            var data = $.parseJSON(data);
            if(data.success){
                $("#client_save_dialog").dialog('close');
                $.messager.alert("温馨提示",data.msg,'info',function () {
                    $("#client_plan_datagrid").datagrid('reload');
                });
            }else{
                $.messager.alert("温馨提示",data.msg,'error');
            }
        }
    });
}


//删除
function remove() {
    var row = $("#client_plan_datagrid").datagrid('getSelected');
    if(!row){
        $.messager.alert("温馨提示","麻烦您选中一条数据！",'error');
        return;
    }
    var id = row.id;
    $.messager.confirm('温馨提示','确定要删除该开发计划吗？',function (data) {
        if(data){
            $.get('/clientPlan/delete.do',{id:id},function (data) {
                if(data.success){
                    $.messager.alert("温馨提示",data.msg,'info',function () {
                        $("#client_plan_datagrid").datagrid('reload');
                    });
                }else{
                    $.messager.alert("温馨提示",data.msg,'error');
                }
            });
        }
    });
}

function query() {
    var name = $("#name").textbox('getValue');
    var inputUser = $("#inputUser").combobox('getValue');
    var beginTime = $("#beginTime").textbox('getValue');
    var endTime = $("#endTime").textbox('getValue');
    $("#client_plan_datagrid").datagrid('load',{
        name:name,
        inputUser_id:inputUser,
        beginTime:beginTime,
        endTime:endTime
    });
}